package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName:ScheduledTasks
 * Package: com.atguigu.lease.web.admin.schedule
 * Description:
 *
 * @Author Makabaka
 * @Create 2024/9/25 10:07
 * @Version 1.0
 */
//定时任务  定时执行相关操作
@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;


    @Scheduled(cron = "0 0 0 * * *")
    public void checkStatus() {

        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.lt(LeaseAgreement::getLeaseEndDate, new Date())
                     .in(LeaseAgreement::getStatus, 2, 5, 7)
                     .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);

        leaseAgreementService.update(updateWrapper);
    }
}
